#!/bin/bash
#会通过 lib lint检查文件正确性
export LANG=en_US.UTF-8

spec_repo_name="RoffaSpecs"
localSpecGitDir="https://gitee.com/roffa/roffa-specs.git"

# 需要打tag的git文件夹路径
git_file=$(dirname $0)
cd ${git_file}
for file in `ls `
do
  if [[ $file =~ \.podspec$ ]];then
    spec_file=$file
    break
  fi
done

echoError(){
    echo -e "\033[31;1m$1\033[0m"
}
echoNormal(){
    echo -e "\033[32;1m$1\033[0m"
}

echoNormal "-----------------lib lint start-----------------"
pod lib lint --verbose --allow-warnings --no-clean --sources=${spec_sources}
if [ $? != 0 ]; then
    echoError "-----------------lib lint error-----------------"
    exit 0

fi
#git tag -l -n

last_tag=$(git describe --tags `git rev-list --tags --max-count=1`)

versionString=`grep -E 's.version.*=' $spec_file`
versionString=${versionString#*\'}
versionString=${versionString%*\'}
echoNormal "-------- 当前项目仓库最新 tag: ${last_tag} -----------------"
echoNormal "-------- 本地podspec tag: ${versionString} -----------------"
echoNormal "新tag:${versionString}(enter确认，手动输入其他终止)"
read newTag

if [ "" == "$newTag" ];then
    newTag=${versionString}
fi

if [ "$versionString" != "$newTag" ];then
    echoError "-----------------已手动终止-----------------"
    exit
fi

if [ "$last_tag" != "$newTag" ];then
    git tag ${newTag}
    git push origin ${newTag}
    if [ $? != 0 ]; then
        echoError "-----------------推送tag失败，请检查网络环境-----------------"
        exit 1
    fi
fi

echoNormal "-----------------spec lint start-----------------"
pod spec lint --verbose --allow-warnings --no-clean  --sources=${spec_sources}
if [ $? != 0 ]; then
    echo "-----------------spec lint error-----------------"
    exit 0

fi
echoNormal "----------------- 确认新的spec版本信息 -----------------------"

echoNormal "tag: "${newTag}

echoNormal "projectFile: "${git_file}

echoNormal "specFile: ${git_file}/${spec_file}"

echoNormal "----------------- 是否开始推送新的spec [输入'y'开始] -----------------------"

read answer
if [[ $answer != y ]]; then
	exit
fi

# 文件夹下需要推到远端的 *.podspec文件
pod repo push $spec_repo_name $spec_file \
--allow-warnings \
--skip-import-validation \
--verbose 


